// pages/donate/donate.js
Page({
  data: {
    currentStep: 1,
    progressPercent: 33,
    formData: {
      name: '',
      gender: '',
      age: '',
      idCard: '',
      phone: '',
      relativeName: '',
      relation: '',
      relativePhone: '',
      address: ''
    },
    genderOptions: ['请选择性别', '男', '女'],
    genderIndex: 0,
    relationOptions: ['请选择关系', '父亲', '母亲', '儿子', '女儿', '配偶', '其他'],
    relationIndex: 0,
    agreed: false,
    canProceed: false,
    showSuccess: false,
    registrationNumber: ''
  },

  onLoad: function (options) {
    console.log('捐献登记页面加载');
  },

  // 检查当前步骤是否可以继续
  checkCanProceed: function() {
    let canProceed = false;
    
    if (this.data.currentStep === 1) {
      canProceed = this.data.formData.name && 
                   this.data.formData.age && 
                   this.data.formData.idCard && 
                   this.data.formData.phone &&
                   this.data.genderIndex > 0;
    } else if (this.data.currentStep === 2) {
      canProceed = this.data.formData.relativeName && 
                   this.data.formData.relativePhone &&
                   this.data.relationIndex > 0;
    }
    
    this.setData({
      canProceed: canProceed
    });
  },

  // 下一步
  nextStep: function() {
    if (!this.data.canProceed) return;
    
    const nextStep = this.data.currentStep + 1;
    const progressPercent = (nextStep / 3) * 100;
    
    this.setData({
      currentStep: nextStep,
      progressPercent: progressPercent
    });
  },

  // 上一步
  prevStep: function() {
    const prevStep = this.data.currentStep - 1;
    const progressPercent = (prevStep / 3) * 100;
    
    this.setData({
      currentStep: prevStep,
      progressPercent: progressPercent
    });
  },

  // 表单输入处理
  onNameInput: function(e) {
    this.setData({
      'formData.name': e.detail.value
    });
    this.checkCanProceed();
  },

  onGenderChange: function(e) {
    this.setData({
      genderIndex: e.detail.value,
      'formData.gender': this.data.genderOptions[e.detail.value]
    });
    this.checkCanProceed();
  },

  onAgeInput: function(e) {
    this.setData({
      'formData.age': e.detail.value
    });
    this.checkCanProceed();
  },

  onIdCardInput: function(e) {
    this.setData({
      'formData.idCard': e.detail.value
    });
    this.checkCanProceed();
  },

  onPhoneInput: function(e) {
    this.setData({
      'formData.phone': e.detail.value
    });
    this.checkCanProceed();
  },

  onRelativeNameInput: function(e) {
    this.setData({
      'formData.relativeName': e.detail.value
    });
    this.checkCanProceed();
  },

  onRelationChange: function(e) {
    this.setData({
      relationIndex: e.detail.value,
      'formData.relation': this.data.relationOptions[e.detail.value]
    });
    this.checkCanProceed();
  },

  onRelativePhoneInput: function(e) {
    this.setData({
      'formData.relativePhone': e.detail.value
    });
    this.checkCanProceed();
  },

  onAddressInput: function(e) {
    this.setData({
      'formData.address': e.detail.value
    });
  },

  onAgreementChange: function(e) {
    this.setData({
      agreed: e.detail.value.length > 0
    });
  },

  // 提交表单
  submitForm: function() {
    if (!this.data.agreed) {
      wx.showToast({
        title: '请先同意协议',
        icon: 'none'
      });
      return;
    }

    // 生成登记编号
    const registrationNumber = 'DN' + Date.now().toString().slice(-8);
    
    // 模拟提交
    wx.showLoading({
      title: '提交中...'
    });

    setTimeout(() => {
      wx.hideLoading();
      
      this.setData({
        showSuccess: true,
        registrationNumber: registrationNumber
      });

      // 保存到本地存储
      const registrationData = {
        ...this.data.formData,
        registrationNumber: registrationNumber,
        submitTime: new Date().toISOString()
      };
      
      wx.setStorageSync('donationRegistration', registrationData);
      
    }, 2000);
  },

  // 关闭成功弹窗
  closeSuccess: function() {
    this.setData({
      showSuccess: false
    });
    
    // 跳转到查询页面
    wx.navigateTo({
      url: '/pages/query/query'
    });
  },

  // 表单验证
  validateForm: function() {
    const { formData } = this.data;
    
    // 验证身份证号
    const idCardRegex = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
    if (!idCardRegex.test(formData.idCard)) {
      wx.showToast({
        title: '身份证号格式不正确',
        icon: 'none'
      });
      return false;
    }
    
    // 验证手机号
    const phoneRegex = /^1[3-9]\d{9}$/;
    if (!phoneRegex.test(formData.phone)) {
      wx.showToast({
        title: '手机号格式不正确',
        icon: 'none'
      });
      return false;
    }
    
    // 验证年龄
    const age = parseInt(formData.age);
    if (age < 18 || age > 100) {
      wx.showToast({
        title: '年龄必须在18-100岁之间',
        icon: 'none'
      });
      return false;
    }
    
    return true;
  },

  // 页面分享
  onShareAppMessage: function() {
    return {
      title: '遗体捐献志愿登记 - 让生命延续',
      path: '/pages/donate/donate'
    };
  }
});
